﻿//-------------------------------------------------------------------------------------------
// <copyright file="Offers.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.Data
{
    using Converters;
    using Ripple.Providers;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// Dataset class for accessing Offer entities.
    /// </summary>
    public class Offers : DataSet
    {
        /// <summary>
        /// Initializes a new instance of the Offers class.
        /// </summary>
        public Offers(IRippleContext context)
            : base(context)
        {
        }

        public IEnumerable<Offer> Get(string accountId)
        {
            List<Offer> resultList = new List<Offer>();

            AccountOffersResult rippleResult = this.Context.Provider.AccountOffers(accountId, this.Context.LedgerIndex);
            while (rippleResult.Success && resultList.Count < this.MaxItems)
            {
                resultList.AddRange(rippleResult.offers.ToEntities(this.Context, rippleResult.account));

                if (rippleResult.marker != null)
                {
                    rippleResult = this.Context.Provider.AccountOffers(accountId, this.Context.LedgerIndex, rippleResult.marker);
                }
                else
                {
                    break;
                }
            }

            return resultList;
        }
    }
}